<?php
namespace Andorra\drivers;

use Andorra\dao\Entity;

class PgsqlDriver extends DataDriver {
	private $conn;
	
	public function __construct(\DOMElement $node) {
		$this->conn = pg_connect($node->textContent);
	}
	
	public function __destruct() {
		pg_close($this->conn);
	}
	
	public function getEntities(Entity $e) {
		$sql = 'SELECT * FROM "' . $e->name . '"';
		$result = pg_query($this->conn, $sql);
		
		return pg_fetch_all($result);
	}
	
	public function getEntityById(Entity $e, $id) {
		$sql = 'SELECT *
				FROM "' . $e->name . '"
				WHERE "' . $e->key . '" = ' . $id;
		$result = pg_query($this->conn, $sql);
		
		return pg_fetch_assoc($result);
	}
}